package com.ttbj.controller.sysuser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.binarywang.java.emoji.EmojiConverter;
import com.ttbj.constant.WxUrilEnum;
import com.ttbj.product.api.AppletUserFacade;
import com.ttbj.product.bean.dto.UserDto;
import com.ttbj.product.bean.dto.ViewData;
import com.ttbj.util.Assert;
import com.ttbj.util.ResultUtil;
import com.ttbj.util.StringUtil;
import com.ttbj.util.ViewModelUtil;

@Controller
@RequestMapping("/applet/user")
public class AppletUserController {
	private static final Logger logger = LoggerFactory.getLogger(AppletUserController.class);

	@Autowired
	AppletUserFacade appletUserFacade = null;

	private EmojiConverter emojiConverter = EmojiConverter.getInstance();

	/**
	 * 小程序登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData Login(HttpServletRequest request, HttpServletResponse response, String code, String gender, String encryptedData, String iv, UserDto userDto) throws Exception {
		Assert.assertNonNullObj(code, 115, "登陆参数出错、原因:code缺失,code");
		Assert.assertNonNullObj(userDto, 115, "登陆参数出错、原因:登陆用户信息(UserDto)为null,登陆用户");
		String nickname = userDto.getNickname();
		if (!StringUtil.isEmpty(nickname)) {
			userDto.setNickname(emojiConverter.toAlias(nickname));
		}
		StringBuffer url = new StringBuffer(WxUrilEnum.WX_URL.getValue()).append(WxUrilEnum.APPID.getValue()).append(WxUrilEnum.SECRET.getValue()).append(WxUrilEnum.CODE.getValue()).append(code).append(WxUrilEnum.TYPE.getValue());
		userDto.distrSex(gender).netOpenId(url.toString());// 性别处理和获取设置opEnId
		UserDto user = appletUserFacade.getUser(userDto);
		if (null == user) {
			appletUserFacade.addUser(userDto);
		} else {
			userDto = user;
		}
		logger.info("用户信息：{}",user);
		return ViewModelUtil.buildSuccess("成功", userDto);
	}

	/**
	 * 获取手机号码验证短信
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/sms.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData userSms(String openId, String phone) throws Exception {
		Assert.assertNonNullObj(phone, 913, "手机号码不存在,phone");
		appletUserFacade.getSms(phone);
		return ViewModelUtil.buildSuccess("成功");
	}
	
	
	/**
	 * 短信验证用户信息是否通过
	 * @throws Exception
	 */
	@RequestMapping(value = "/examine/sms.do", method = RequestMethod.POST)
	@ResponseBody
	public ViewData examineSMS(String openId, String phone, String code) throws Exception {
		Assert.assertNonNullObj(phone, 913, "手机号码不存在,phone");
		Assert.assertNonNullObj(code, 914, "手机验证码不存在,手机验证码");
		boolean isPass = appletUserFacade.verifySms(phone, code);
		if(!isPass){
			logger.info("输入验证码不匹配");
			return ResultUtil.createFail("messages", 914);
		}
		appletUserFacade.updateUser(new UserDto(openId,phone));
		return  ViewModelUtil.buildSuccess("成功");
		
	}
	

}
